package gui.toolpanels;


import gui.FileTable;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.util.concurrent.ExecutorService;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.event.TreeExpansionEvent;
import javax.swing.event.TreeExpansionListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;

import utils.SSHUtils;
import utils.Utils;


public class FileTreePanel extends JPanel implements TreeSelectionListener {
	private JTree tree;
	private DefaultTreeModel model;
	private static boolean DEBUG = true;
	
	// Optionally play with line styles. Possible values are
	// "Angled" (the default), "Horizontal", and "None".
	private static boolean playWithLineStyle = true;
	private static String lineStyle = "Horizontal";
	//private Test mp;
	public FileTreePanel() {
		super();
		
		model = new DefaultTreeModel(Utils.root);
		createNodes(Utils.root);
		// Create a tree that allows one selection at a time.
		tree = new JTree(model);
		tree.getSelectionModel().setSelectionMode(
				TreeSelectionModel.SINGLE_TREE_SELECTION);
		tree.addTreeExpansionListener(new MyTreeExpansionListener());
		// Listen for when the selection changes.
		tree.addTreeSelectionListener(this);

		if (playWithLineStyle) {
			//System.out.println("line style = " + lineStyle);
			tree.putClientProperty("JTree.lineStyle", lineStyle);
		}
		// Add the split pane to this panel.
		add(tree);
	}

	/** Required by TreeSelectionListener interface. */
	public void valueChanged(TreeSelectionEvent evt) {
		DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree
				.getLastSelectedPathComponent();

		if (node == null)
			return;

		Object nodeInfo = node.getUserObject();
		if (node.isLeaf()) {
		} else {
			final JTree tree = (JTree) evt.getSource();
			final TreePath path = evt.getPath();
			javax.swing.SwingUtilities.invokeLater(new Runnable() {
				public void run() {
					final CNode node = (CNode) path.getLastPathComponent();
					CFile cf = (CFile) node.getFile();
					if (cf.children.isEmpty()) {

						SwingUtilities.invokeLater(new Runnable() {
							public void run() {
								String dir = "~";
								for (int i = 1; i < path.getPath().length; i++) {
									dir += "/" + path.getPath()[i].toString();
									System.out.println("dir " + dir);
								}
								SSHUtils.buildFileSystem(dir, node);
								createNodes(node);
								//mp.setFileListPanel(node.getPanel());
							}
						});
					}//else mp.setFileListPanel(node.getPanel());
					// ((DefaultTreeModel) tree.getModel()).reload();
					System.out.println("treeSel");
				}});
		}
		if (DEBUG) {
			System.out.println(nodeInfo.toString());
		}
	}
	
	public void insert(CNode file)
	{
		model.insertNodeInto(file, Utils.root, 0);
		revalidate();
	}

	private void createNodes(CNode top) {
		CFile cf = (CFile) top.getFile();
		/*if (cf == null || cf.children.isEmpty())
		{
			FileTable.GenerateTable(top);
			return;
		}*/
		int c = 0;
		if (cf.isDir)
			top.removeAllChildren();
		for (gui.toolpanels.CFile f : cf.children) {
			if (f.isDir) {
				CNode file = new CNode(f.name);
				file.setFile(f);
				top.add(file);
				file.add(new DefaultMutableTreeNode());
				if (!top.isRoot())
					model.insertNodeInto(file, top, c++);
			}
		}
		for (gui.toolpanels.CFile f : cf.children) {
			if (!f.isDir) {
				CNode file = new CNode(f.name);
				file.setFile(f);
				top.add(file);
				if (!top.isRoot())
					model.insertNodeInto(file, top, c++);
			}
		}
		/*if (cf.isDir)
		{
			FileTable.GenerateTable(top);
		}*/
	}

	class MyTreeExpansionListener implements TreeExpansionListener {
		public void treeExpanded(TreeExpansionEvent evt) {
			final JTree tree = (JTree) evt.getSource();
			final TreePath path = evt.getPath();
			javax.swing.SwingUtilities.invokeLater(new Runnable() {
				public void run() {
					System.out.println("treeExpanded");
					final CNode node = (CNode) path.getLastPathComponent();
					CFile cf = (CFile) node.getFile();
					if (cf.children.isEmpty()) {

						SwingUtilities.invokeLater(new Runnable() {
							public void run() {
								String dir = "~";
								for (int i = 1; i < path.getPath().length; i++) {
									dir += "/" + path.getPath()[i].toString();
									System.out.println("dir " + dir);
								}
								SSHUtils.buildFileSystem(dir, node);
								createNodes(node);
							}
						});
					}
					System.out.println("treeExpanded");
				}
			});
		}

		public void treeCollapsed(TreeExpansionEvent evt) {
			// JTree tree = (JTree) evt.getSource();

			// TreePath path = evt.getPath();
			// System.out.println("treeCollapsed");
		}
	}
}

